<?php

namespace App\Http\Controllers\Web;

use App\ConstDir\ErrorConst;
use App\Repository\Models\OrderProducts;
use App\Repository\Models\Orders;
use App\Repository\Models\Products;
use App\Repository\Repositories\Interfaces\AddressRepository;
use App\Repository\Repositories\Interfaces\CartsRepository;
use App\Repository\Repositories\Interfaces\CompanyRepository;
use App\Repository\Repositories\Interfaces\InvoicesRepository;
use App\Repository\Repositories\Interfaces\OrderExpressRepository;
use App\Repository\Repositories\Interfaces\OrderProductCommentRepository;
use App\Repository\Repositories\Interfaces\OrderProductsRepository;
use App\Repository\Repositories\Interfaces\OrdersRepository;
use App\Repository\Repositories\Interfaces\ProductSkusRepository;
use App\Repository\Repositories\Interfaces\ProductsRepository;
use App\Repository\Repositories\Interfaces\UserInvoiceRepository;
use App\Services\Api\KuaiDi100;
use Illuminate\Http\Request;

/**
 * 订单管理控制器
 *
 * @package App\Http\Controllers\Backend
 */
class ProductsController extends BaseController
{
    /**
     * @var ProductsRepository
     */
    protected $products_repo;

    public function __construct(ProductsRepository $products_repo)
    {
        $this->products_repo = $products_repo;
        //用户所能看到的商品皆为已审核通过且上架的商品
//         $this->products_repo->scopeQuery(function ($query) {
//             return $query->where('status', Products::STATUS_CHECKED)->where('is_for_sale', Products::IS_FOR_SALE_YES);
//         });
    }

    public function index(){
        $products = $this->products_repo->scopeQuery(function($sql){
            return $sql->where('status', Products::STATUS_CHECKED)->where('is_for_sale', Products::IS_FOR_SALE_YES);
        })->paginate($this->perPage);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $products, true);
    }

    public function show($id){
        $product = $this->products_repo->scopeQuery(function($sql){
            //return $sql->where('status', Products::STATUS_CHECKED)->where('is_for_sale', Products::IS_FOR_SALE_YES);
            return $sql->where('is_for_sale', Products::IS_FOR_SALE_YES);
        })->with('skus')->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $product['data']);
    }

    /**
     * 商品评论列表
     * @param OrderProductCommentRepository $order_product_comment
     * @param $product_id
     * @return \Illuminate\Support\Collection
     */
    public function indexProductsComments(OrderProductCommentRepository $order_product_comment, $product_id){
        $comments = $order_product_comment->scopeQuery(function($query) use ($product_id){
            return $query->where('product_id', $product_id);
        })->paginate($this->perPage);

        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $comments);
    }


    public function attrishow($id){
          $product = $this->products_repo->scopeQuery(function($sql){
              return $sql->where('status', Products::STATUS_CHECKED)->where('is_for_sale', Products::IS_FOR_SALE_YES);
          })->with(['skus','attrs'])->find($id);
          return $this->success(ErrorConst::SUCCESS_CODE_MSG, $product['data']);
      }

}
